package com.ceba.test.modules.regist.dao;


import java.util.ArrayList;
import java.util.List;

import com.ceba.test.base.enums.RegisterLogTypeEnum;
import com.ceba.test.base.helper.SystemHelper;
import com.ceba.base.web.dao.IBaseDao;
import com.ceba.test.modules.regist.entity.EnterpriseRegisterLog;

import abc.ID;
import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;

public class EnterpriseRegistLogDao extends IBaseDao {

	private String tableName = "g_e_regist_log";
	/**
	 * 添加一条记录
	 * @return
	 */
	public Boolean add(EnterpriseRegisterLog elog) throws Exception{
		IStore store = StoreManager.create(tableName);
		IObject object = new XObject(tableName);
		object.put("ID",ID.getGuid());
		object.put("G_ENTERPRISE_NAME",elog.getEnterpriseName());
		object.put("NEW_AVALIABLE_DATE",elog.getNewAvailableDate());
		object.put("NEW_SIGN_COUNT",elog.getNewSignCount());
		object.put("LAST_AVALIABLE_DATE",elog.getLastAvailableDate());
		object.put("LAST_SIGN_COUNT",elog.getLastSignCount());
		object.put("MESSAGE",elog.getMessage());
		object.put("TYPE",elog.getType());
		object.setCreateTime();
		object.setCreateUser();
		if(store.insert(object)>0){
			return true;	
		}
		return false;
	}
	
	
	/**
	 * 删除一条数据
	 * @param id 数据id
	 * @return
	 */
	public Boolean delete(String id) throws Exception{
		Sql sql = new Sql("DELETE FROM "+tableName+" WHERE ID = ?");
		sql.addParameter(id);
		return SqlSessionManager.execute(sql);
	}
	
	private void copyDrToEnterpriseRegisterLog(DataRow dr,EnterpriseRegisterLog er){
		er.setId(dr.getString("ID"));
		er.setEnterpriseName(dr.getString("G_ENTERPRISE_NAME"));
		er.setLastAvailableDate(dr.getString("LAST_AVALIABLE_DATE"));
		er.setLastSignCount(dr.getString("LAST_SIGN_COUNT"));
		er.setNewAvailableDate(dr.getString("NEW_AVALIABLE_DATE"));
		er.setNewSignCount(dr.getString("NEW_SIGN_COUNT"));
		er.setMessage(dr.getString("MESSAGE"));
		er.setType(dr.getInt("Type"));
		er.setCreateTime(dr.getString("CREATE_TIME"));
		er.setCreateUser(dr.getString("CREATE_USER"));
	}
	/**
	 * 獲取所有更新信息 RegisterLogTypeEnum = 1、2 、3
	 * @return
	 */
	public List<EnterpriseRegisterLog> getEnterpriseRegisterLogList(String feeType){
		List<EnterpriseRegisterLog> logs = new ArrayList<EnterpriseRegisterLog>();
		String companyName = SystemHelper.getCompanyName();
		String sqlS = " select * from "+tableName +" WHERE G_ENTERPRISE_NAME = '"+companyName
				+"' and FEE_TYPE="+feeType
				+" and TYPE IN("+RegisterLogTypeEnum.INIT.getType()+","
				+RegisterLogTypeEnum.NEXT_UPDATE.getType()+","
				+RegisterLogTypeEnum.CHANGE_TO_FORAML.getType()+") order By CREATE_TIME DESC";
		DataTable dt = SqlSessionManager.getDataTable(sqlS);
		if(dt !=null && dt.getRowCount() >0){
			for( DataRow dr:dt){
				EnterpriseRegisterLog er = new EnterpriseRegisterLog();
				copyDrToEnterpriseRegisterLog(dr,er);
				logs.add(er);
			}
		}
		return logs;
	}
}
